package com.eairlv.route.server.controller;

import com.eairlv.route.server.entity.Result;
import com.eairlv.route.server.entity.form.AuthenticationForm;
import com.eairlv.route.server.entity.vo.AuthenticationVO;
import com.eairlv.route.server.service.AuthenticationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author eairlv
 * @description
 * @date 11:15 2019/10/16
 */
@Slf4j
@Api(tags = "路由认证接口")
@RestController
@RequestMapping("/route/server/authentication")
public class AuthenticationController {

    public static final int SESSION_LENGTH = 32;

    @Autowired
    AuthenticationService authenticationService;

    @GetMapping("/{session}")
    @ApiOperation("Agent端通过配置的session获取分布式路由服务端存储的验证信息")
    public Result<AuthenticationVO> session(@PathVariable String session) {
        if (session.length() == SESSION_LENGTH){
            return authenticationService.session(session);
        } else {
            return Result.failMessage("invalid session");
        }
    }

    @PostMapping
    @ApiOperation("添加认证信息")
    public Result authentication(@RequestBody AuthenticationForm authenticationForm) {
        return authenticationService.authentication(authenticationForm);
    }

    @PutMapping
    @ApiOperation("修改认证信息")
    public Result updateAuthentication(@RequestBody AuthenticationForm authenticationForm) {
        return authenticationService.updateAuthentication(authenticationForm);
    }
}
